//
//  KLDoctorListViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLCarMessageTableViewCell.h"
#import "KLCarMessageViewController.h"
#import "KLSendCarViewController.h"
#import "KLCarDetailViewController.h"
@interface KLCarMessageViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation KLCarMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"货车信息";
    [self st_setRightItemWithTitle:@"发布信息" titleColor:[UIColor whiteColor]];
    [self configSubView];
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无信息" buttonTitle:@"发布信息" buttonHandle:^(NSString *titleString) {
        [weakSelf st_rightBarAction:nil];
    }];
    
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendWeekListRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLCarMessageTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLCarMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLCarMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    
    cell.model = self.dataSouce[indexPath.section];
    __weak typeof(self) weakSelf =  self;
    [cell.delButton setClicAction:^(UIButton *sender) {
        [weakSelf sendDelCarMessageWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)sendDelCarMessageWithIndexPath:(NSIndexPath*)indexPath{
   KLCarModel *  model = self.dataSouce[indexPath.section];
    [self showAlertCancelAndConfirm:@"请问要删除这条货车信息吗?" andWithBlock:^(NSString *name) {
        if ([name isEqualToString:@"确认"]) {
            [self sendDelRequestWithModel:model];
        }
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KLCarModel * model = self.dataSouce[indexPath.section];
    KLCarDetailViewController * vc = [KLCarDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    
    [self.navigationController pushViewController:[KLSendCarViewController new] animated:YES];
    
}

#pragma mark --NetWork Method
- (void)sendWeekListRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/freight/freightlist"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * array = dataDic[@"list"];
                                                     self.dataSouce = [NSMutableArray new];
                                                     for (NSDictionary * modelDic in array) {
                                                         KLCarModel * model = [KLCarModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
- (void)sendDelRequestWithModel:(KLCarModel*)model{
    NSMutableDictionary * dic = [NSMutableDictionary new];
     NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/freight/freight_del",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    if (model.freight_id.description) {
        [dic setObject:model.freight_id forKey:@"freight_id"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD showSuccessWithStatus:@"删除成功"];
                                                 [self.tableView.mj_header beginRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];

                                             }];
}
@end



